<?php

namespace App\Http\Controllers\Customer;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use ValiDate;
use App\Facades\Services\CustomerService;
use App\Rules\PhoneRule;

class CustomerController extends Controller
{
    public function login(Request $request){

    	$rules = [
    		'phone' => ['required',new PhoneRule ,'exists:customers,phone'],
    		'password' => 'required'
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	CustomerService::login($data);

    	return formatJsonResponse();
    }

    public function codeLogin(Request $request){
    	$rules = [
    		'phone' => ['required',new PhoneRule ,'exists:customers,phone'],
    		'code' => 'required'
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	CustomerService::codeLogin($data);

    	return formatJsonResponse();
    }

    

    public function forgetPwd(Request $request){

    	$data = $request->all();

    	$rules = [
    		'phone' => ['required',new PhoneRule ,'exists:customers,phone'],
    		'newPassword' => 'required',
    		'confirmPassword' => 'required|same:newPassword',
    		'code' => 'required'
    	];

    	ValiDate::validate($request,$rules);

    	CustomerService::forgetPwd($data);

    	return formatJsonResponse();
    }

    public function register(Request $request){

    	$rules = [
    		'phone' => ['required',new PhoneRule ,'unique:customers,phone'],
    		'password' => 'required',
    		'confirmPassword' => 'required|same:password',
            'code' => 'required'
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	CustomerService::register($data);

    	return formatJsonResponse();
    }

    public function getCode(Request $request){
    	$rules = [
    		'phone' => ['required',new PhoneRule ,'exists:customers,phone']
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	return formatJsonResponse(CustomerService::getCode($data));
    }

    public function getForgetCode(Request $request){
    	$rules = [
    		'phone' => ['required',new PhoneRule ,'exists:customers,phone']
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	return formatJsonResponse(CustomerService::getForgetCode($data));
    }

    public function getRegisterCode(Request $request){
    	$rules = [
    		'phone' => ['required',new PhoneRule ,'unique:customers,phone']
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	return formatJsonResponse(CustomerService::getRegisterCode($data));
    }

    public function updatePwd(Request $request){
    	$rules = [
    		'password' => 'required',
    		'newPassword' => 'required',
    		'confirmPassword' => 'required|same:newPassword'
    	];

    	ValiDate::validate($request,$rules);

    	$data = $request->all();

    	CustomerService::updatePwd($data);

    	return formatJsonResponse();
    }

    public function logout(){
    	Auth::logout();
    }
}
